package com.xindaoapp.henghua.utils;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xindaoapp.henghua.R;
import com.xindaoapp.henghua.application.XinDaoApplication;
import com.xindaoapp.henghua.entry.PropertyGatherInfo.MODIFIEDTYPE;
import com.xindaoapp.henghua.entry.PropertyLabelInfo;
import com.xindaoapp.henghua.entry.ScanData;

@SuppressLint("ResourceAsColor")
public class SwipeAdapter extends BaseAdapter {
	private Context mContext = null;
	private final List<PropertyLabelInfo> labelList;
	// 设备的扫描记录
	private List<ScanData> data;

	public SwipeAdapter(Context ctx, List<PropertyLabelInfo> data) {
		mContext = ctx;
		this.labelList = data;
	}

	@Override
	public int getCount() {
		return labelList.size();
	}

	@Override
	public Object getItem(int position) {
		return labelList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		if (null == convertView) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.fragment_label_item, null);
			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.tv_place = (TextView) convertView.findViewById(R.id.tv_place);
			holder.tv_terminal = (TextView) convertView.findViewById(R.id.tv_terminal);
			holder.tv_location = (TextView) convertView.findViewById(R.id.tv_location);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_label_delete);
			holder.ll_label_item = (LinearLayout) convertView.findViewById(R.id.ll_label_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		int pos = position;
		PropertyLabelInfo label = labelList.get(pos);

		if (label.isModified == MODIFIEDTYPE.MODIFIED.ordinal()) {
			holder.tv_num.setTextColor(0xff0000ff);
			holder.tv_place.setTextColor(0xff0000ff);
			holder.tv_terminal.setTextColor(0xff0000ff);
			holder.tv_location.setTextColor(0xff0000ff);
			holder.tv_time.setTextColor(0xff0000ff);

		}

		holder.tv_num.setText(label.tag_code);// 标签编码
		holder.tv_place.setText(label.part);// 贴标签位置

		String time = CommFunc.getTime(label.create_time);

		holder.tv_time.setText(time);// 贴标签时间

		String str = XinDaoApplication.get().getUserInfo().scan_devicename;

		if (!TextUtils.isEmpty(str)) {
			holder.tv_terminal.setText(str);
		}

		String place = getSacanLocation(label.tag_code);

		holder.tv_location.setText(place);

		holder.iv_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onRightItemClick(v, position);
				}

				if (mItem != null) {
					mItem.getRightItem(v, position);
				}
			}
		});

		if (position % 2 == 0) {
			convertView.setBackgroundColor(Color.parseColor("#ffffff"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#eff8fe"));
		}

		return convertView;
	}

	@SuppressLint("NewApi")
	private String getSacanLocation(String tag_code) {
		if (data != null) {
			for (int i = 0; i < data.size(); i++) {
				if (data.get(i).tag_code.equals(tag_code)) {
					String strAddr = data.get(i).scan_location;

					if (!strAddr.isEmpty()) {
						return strAddr;
					} else {
						String strGps = data.get(i).scan_location_gps;
						if (!strGps.isEmpty()&& !strGps.equals("(4.9E-324,4.9E-324)")) {
							return strGps;
						}else{
							return "位置未取到";
						}
					}
				}
			}
		}

		return "";
	}

	static class ViewHolder {
		TextView tv_num, tv_place, tv_terminal, tv_location, tv_time;
		ImageView iv_delete;
		LinearLayout ll_label_item;
	}

	/**
	 * 单击事件监听器
	 */
	private onRightItemClickListener mListener = null;

	public void setOnRightItemClickListener(onRightItemClickListener listener) {
		mListener = listener;
	}

	public interface onRightItemClickListener {
		void onRightItemClick(View v, int position);
	}

	/**
	 * 获取右边按钮
	 */
	private getRightItem mItem = null;

	public void getRightItem(getRightItem item) {
		mItem = item;
	}

	public interface getRightItem {
		void getRightItem(View v, int position);
	}

	public void setScanDatas(List<ScanData> data) {
		this.data = data;
	}

}
